using System;
using System.CodeDom;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using Fcdbas.WinUI.Windows;

namespace Fcdbas.WinUI.Design
{
	internal class SalNamedPropertiesSerializer : CodeDomSerializer
	{
		public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
		{
			return null;
		}
		public override object Serialize(IDesignerSerializationManager manager, object value)
		{
			SalNamedProperties salNamedProperties = SalNamedPropertiesEditor.SafeCast(value);
			if (salNamedProperties == null)
			{
				return null;
			}
			CodeStatementCollection codeStatementCollection = base.Serialize(manager, value) as CodeStatementCollection;
			if (codeStatementCollection == null)
			{
				codeStatementCollection = new CodeStatementCollection();
			}
			CodeExpression codeExpression = base.SerializeToExpression(manager, value);
			CodePropertyReferenceExpression codePropertyReferenceExpression = codeExpression as CodePropertyReferenceExpression;
			if (codePropertyReferenceExpression == null)
			{
				return null;
			}
			PropertyDescriptor property = (PropertyDescriptor)manager.Context[typeof(PropertyDescriptor)];
			SalNamedProperties originalValue = this.GetOriginalValue(property);
			codeStatementCollection.Clear();
			CodeMethodReferenceExpression method = new CodeMethodReferenceExpression(codePropertyReferenceExpression, "Put");
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList(salNamedProperties.Keys);
			arrayList.Sort();
			foreach (string text in arrayList)
			{
				string text2 = salNamedProperties[text];
				if (text2 != null && (originalValue == null || !text2.Equals(originalValue[text])))
				{
					CodeExpression value2 = new CodePrimitiveExpression(text);
					CodeExpression value3 = new CodePrimitiveExpression(text2);
					codeStatementCollection.Add(new CodeMethodInvokeExpression
					{
						Method = method,
						Parameters = 
						{
							value2,
							value3
						}
					});
				}
			}
			return codeStatementCollection;
		}
		private SalNamedProperties GetOriginalValue(PropertyDescriptor property)
		{
			InheritedPropertyDescriptor inheritedPropertyDescriptor = property as InheritedPropertyDescriptor;
			if (inheritedPropertyDescriptor != null)
			{
				System.Collections.IList list = inheritedPropertyDescriptor.OriginalValue as System.Collections.IList;
				if (list != null)
				{
					SalNamedProperties salNamedProperties = new SalNamedProperties();
					foreach (System.Collections.DictionaryEntry dictionaryEntry in list)
					{
						salNamedProperties[(string)dictionaryEntry.Key] = (string)dictionaryEntry.Value;
					}
					return salNamedProperties;
				}
			}
			return null;
		}
	}
}
